/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Conn.mysqlconnect;
import ModelKelas.Pendaftaran;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AnggitDwiantoro
 */
public class ControlPendaftaran {

    ResultSet rs = null;
    PreparedStatement pst = null;
    private Connection conn;

    public ControlPendaftaran(Connection koneksi) {
        this.conn = koneksi;
    }

    public static ControlPendaftaran getkoneksi() throws SQLException {
        ControlPendaftaran kon = new ControlPendaftaran(mysqlconnect.ConnectDb());

        return kon;
    }

    public void tambahPendaftaran(Pendaftaran pen) throws SQLException {

        String namaPasien = pen.getNamaPasien();
        String alamatPasien = pen.getAlamatPasien();
        String nomorPendaftaran = pen.getNomorPendaftaran();
        String Tanggal = pen.getTanggal();
        String noIndex = pen.getNo_Index();
        String sql = "INSERT INTO Pendaftaran (No_Pendaftaran ,Nama_Pasien, Alamat, Tanggal,Retribusi,No_Index) VALUES "
                + "('" + nomorPendaftaran + "','" + namaPasien + "','" + alamatPasien
                + "','" + Tanggal + "','" + 3500 + "','" + noIndex + "')";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
    }

    public void cariPendaftaran(Pendaftaran pen) throws SQLException {
        Statement statement = null;
        ResultSet result = null;

        try {
            statement = conn.createStatement();
            String sql = "select pasien.noIndex, pasien.namaPasien, pasien.alamat "
                    + "from pasien "
                    + "where namaPasien= '" + pen.getNamaPasien() + "'";
            result = statement.executeQuery(sql);
            while (result.next()) {
                pen.setNo_Index(result.getString("noIndex"));
                pen.setNamaPasien(result.getString("namaPasien"));
                pen.setAlamatPasien(result.getString("alamat"));
            }
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(ControlPendaftaran.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    public List<Pendaftaran> caribarang(String nama) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet rset = null;
        conn.setAutoCommit(false);
        String sql = "select pendaftaran.No_Pendaftaran, pendaftaran.Nama_Pasien, pendaftaran.Alamat "
                + "from pendaftaran "
                + " where pendaftaran.Nama_Pasien like '%" + nama + "%'"
                + " order by pendaftaran.No_Pendaftaran";
        psmt = conn.prepareCall(sql);
        rset = psmt.executeQuery();
        List<Pendaftaran> pend = new ArrayList<Pendaftaran>();
        while (rset.next()) {
            Pendaftaran p = new Pendaftaran();
            p.setNomorPendaftaran(rset.getString("No_Pendaftaran"));
            p.setNamaPasien(rset.getString("Nama_Pasien"));
            p.setAlamatPasien(rset.getString("Alamat"));
            pend.add(p);
        }
        conn.commit();
        return pend;
    }

    public List<Pendaftaran> lihatSeluruhPendaftaran() throws SQLException {
        Statement statement = null;
        ResultSet result = null;
        statement = conn.createStatement();
//            conn.setAutoCommit(false);
            String sql = ("select No_Pendaftaran, Nama_Pasien, Alamat"
                    + " from Pendaftaran order by No_Pendaftaran;");
            result = statement.executeQuery(sql);
            List<Pendaftaran> pendaf = new ArrayList<Pendaftaran>();
            while (result.next()) {
                Pendaftaran pendaftaran = new Pendaftaran();
                pendaftaran.setNomorPendaftaran(result.getString("No_Pendaftaran"));
                pendaftaran.setNamaPasien(result.getString("Nama_Pasien"));
                pendaftaran.setAlamatPasien(result.getString("Alamat"));
                pendaf.add(pendaftaran);
            }
            conn.close();
            return pendaf;
    }
}
